package com.fxd.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fxd.common.result.Result;
import com.fxd.model.system.SysRole;
import com.fxd.model.vo.SysRoleQueryVo;
import com.fxd.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll(){
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole){
        try{
            sysRoleService.save(sysRole);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysRole sysRole) {
        try{
            sysRoleService.updateById(sysRole);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
        return Result.ok();
    }
    @ApiOperation(value = "根据id删除角色")
    @DeleteMapping("/deleteById/{id}")
    public Result delete(@PathVariable Long id){
        boolean isDelete = sysRoleService.removeById(id);
        if (isDelete){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }
    //条件分页查询
    @ApiOperation(value = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo roleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }

}
